<?php
//
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'main';
if($_RQ['lang']){
	$_SESSION['show_lang']=$_RQ['lang'];
}

//检查异常订阅
if($_SESSION['subscribeid']){
	if(function_exists('uperror_publish')){
		uperror_publish($_SESSION['subscribeid']);
	}
}
if($_RQ['op']=='main'){
	//未登录下单
	$return['ischecklogin']=model('jy_weishop/member')->checklogin('','','','',true);
	$return['registerurl']=$return['pluginset']['app_saleurl']?:'';
	$return['show_setmobile']=cfc('core')->check_setmobile(2);
	$return['show_setmember']=cfc('core')->check_setmember(2);
	$return=model('jy_weishop/page/order_list')->getmain($return);
	exi($return);
}
// js 获取数据
if($_RQ['op']=='getlist'){
	$params=array('uniacid'=>$_SESSION['uniacid'],'deleted'=>0,'userdeleted'=>0);
	$params['plugin_name']='jy_kongtiaoshop';
	$_RQ['api_data']=1;
	$list=model('jy_weishop/get_order')->getlist($params,$_RQ);
	foreach($list as &$item){
		$peigoods=$item['api_data']['peiinfo']['goods'];
		$allwfee=0;
		foreach($peigoods as $k=>&$gv){
			$goodsinfo=pdo_get('jy_weishop_goods',array('uniacid'=>$_SESSION['uniacid'],'id'=>$gv['goodsid']));
			$api_data=json_decode($goodsinfo['api_data'],true);
			$gv['title']=$goodsinfo['title'];
			$gv['image']=$goodsinfo['image'];
			$gv['price']=$goodsinfo['price'];
			$gv['goodssn']=$api_data['sn'];
			$gv['wfee']=$api_data['wfee'];
			$gv['onewfee']=price($api_data['wfee']/$gv['mianji']);
			$allwfee +=$gv['wfee'];
		}
		$item['allwfee']=$allwfee;
		$item['peigoods']=$peigoods;
		if($item['api_data']['peiinfo']['waijiid']){
			$waiji=array();
			$goodsinfo=pdo_get('jy_weishop_goods',array('uniacid'=>$_SESSION['uniacid'],'id'=>$item['api_data']['peiinfo']['waijiid']));
			$api_data=json_decode($goodsinfo['api_data'],true);
			$peibi=$api_data['wfee']/$allwfee;
			$waiji[]=array(
				'title'=>$goodsinfo['title'],
				'image'=>$goodsinfo['image'],
				'price'=>$goodsinfo['price'],
				'goodssn'=>$api_data['sn'],
				'total'=>1,
				'wfee'=>$api_data['wfee'],
				'peibi'=>price($peibi),
			);
		}
		$item['waiji']=$waiji;
		$kongjian=$item['api_data']['peiinfo']['kongjian'];
		foreach($kongjian as $k=>&$gv){
			$goodsinfo=pdo_get('jy_weishop_goods',array('uniacid'=>$_SESSION['uniacid'],'id'=>$gv['goodsid']));
			$api_data=json_decode($goodsinfo['api_data'],true);
			$gv['title']=$goodsinfo['title'];
			$gv['image']=$goodsinfo['image'];
			$gv['price']=$goodsinfo['price'];
			$gv['goodssn']=$api_data['sn'];
		}
		$item['kongjian']=$kongjian;
		$item['status_title']=$this->order_status[$item['status']];
	}
	$return['list']=$list;
	exi($return);
}
if($_RQ['op']=='pay'){
	$id = intval($_RQ['id']);
	if(!$id){
		exi('订单不存在，请重新选择订单','error');
	}
	$order=model('jy_weishop/order')->get_order($id);
	foreach($order['goods'] as $gv){
		model('jy_weishop/goods')->check_buy($gv['goodsid'],$gv['total'],$gv['optionid']);
	}
	if($order['ding_price']>0){
		$dingorder=pdo_get('jy_weishop_order_ding',array('uniacid'=>$_SESSION['uniacid'],'orderid'=>$id));
		if($dingorder['status']!=2){
			exi('该订单的保证金还没交或还未成单，不能支付余款！','error');
		}
	}
	exi($order['pay_id']);
}
if($_RQ['op']=='cancel'){
	$post=get_postdata();	
	$id=intval($post['id']);
	if($id){
		model('jy_weishop/order')->order_cancel($id);
		exi('取消成功！','','/order.list/');		
	}else{		
		exi('取消失败！','error');
	}

}
if($_RQ['op']=='receive'){
	$post=get_postdata();
	$id=intval($post['id']);
	model('jy_weishop/order')->order_receive($id);
	exi('收货成功！');	
}
//支付订单
if($_RQ['op']=='payaddfee'){
	$id=intval($_RQ['id']);
	$order=pdo_get('jy_weishop_order',array('id'=>$id));
	$isorder=pdo_get('jy_weishop_order_addfee',array(
			'uniacid'=>$_SESSION['uniacid'],
			'mid'=>$order['mid'],
			'orderid'=>intval($order['id'])
			)
	);
	if($isorder['id']){
		$payid=$isorder['pay_id'];
		$order_id=$isorder['id'];
	}else{
		$order_id=model('jy_weishop/order')->createaddfee_order($order);
		$params=array(
			'id'=>$order_id,
			'title'=>'购买商品',
			'fee'=>$order['addprice'],
			//'pay_type'=>'wechat',//可选，此项作用为指定支付方式
			'no_type'=>array('hdfk'),//可选，此项作用为指定不允许使用哪种支付方式
			'back_url'=>'jy_kongtiaoshop/order.list/addpayresult',//必填，指定异步支付结果通知地址，采用系统标准URL书写格式
			'success_url'=>$success_url?:'jy_kongtiaoshop/order.list/',
			'error_url'=>$error_url?:'jy_kongtiaoshop/order.list/',
		);
		$payid=cfc('pay')->create_pay_order($params);
		pdo_update('jy_weishop_order_addfee',array('pay_id'=>$payid),array('id'=>$order_id));
	}
	$res=array(
		'pay_id'=>$payid,
		'order_id'=>$order_id,
		'success_url'=>$success_url,
		'error_url'=>$error_url,
	);
	exi($res);
}
if($_RQ['op']=='addpayresult'){
	$payresult=$_SESSION['pay_result'];
	if($payresult['status']!=1 && $payresult['pay_type'] !='hdfk'){
		exi('支付失败。','error','jy_kongtiaoshop/order.list/');
	}
	$order=pdo_get('jy_weishop_order_addfee',array('id'=>$payresult['id']));
	if($order['ispay'] ==1){
		exi('该订单已经支付！','error','jy_kongtiaoshop/order.list/');
	}
	if($order['price']!=$payresult['fee']){
		exi('支付失败！订单金额不匹配！'.$order['price'].'|'.$payresult['fee'],'error','jy_kongtiaoshop/order.list/');
	}
	$params=array(
		'paytype'=>$payresult['pay_type'],
		'transid'=>$payresult['transaction_id'],
	);
	model('jy_weishop/order')->order_addfeepay($order['id'],$params);
	exi('提交成功!');
}
if($_RQ['op']=='buyto'){
	$orderid=intval($_RQ['orderid']);
	$order=pdo_get('jy_weishop_order',array('id'=>$orderid));
	$storeid=$order['storeid'];
	$merchid=$order['merchid'];
	$sendtype=$order['sendtype'];
	$is_dish=$order['is_dish'];
	$cart_list=pdo_getall('jy_weishop_order_goods',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'orderid'=>$orderid),array('id','goodsid','total','optionid','merchid'));
	if(!$cart_list){
		exi('该订单没有商品！','error');
	}
	foreach($cart_list as &$cart){
			$goods=pdo_get('jy_weishop_goods',array('id'=>$cart['goodsid']),array('id'));
			if(!$goods){
				exi('指定商品不存在！','error');
			}
			//$this->model('goods')->check_buy($goods['id'],$cart['total'],$cart['optionid']);
	}
	$_SESSION['cart_list']=$cart_list;
	if($_SESSION['current_plugin']=='jy_weidish' || $_SESSION['current_plugin']=='jy_weishoppei' || $_SESSION['current_plugin']=='jy_weishoppeishop' || $_SESSION['current_plugin']=='jy_weishopscale'){
		if($_SESSION['current_plugin']=='jy_weishoppeishop'){
			exi('','','jy_weishoppei/create//isbuyto=1');
		}elseif($_SESSION['current_plugin']=='jy_weishopscale'){
			exi('','','jy_weishopscale/create//isbuyto=1');
		}else{
			exi('','',$_SESSION['current_plugin'].'/create//isbuyto=1');
		}
	}else{
		exi('','','jy_kongtiaoshop/order.create//isbuyto=1&storeid='.$storeid.'&merchid='.$merchid.'&sendtype='.$sendtype.'&is_dish='.$is_dish);
	}
}
?>